package com.zlj.jdbc.demo;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * v1.0版本
 * @author bashen
 *
 */
public class JdbcUtils {
	private static Properties props = null;
	
	/*
	 * static静态块中的代码只在JdbcUtils类被加载的时候执行一次
	 */
	static{
		InputStream stream = JdbcUtils.class.getClassLoader().getResourceAsStream("dbConfig.properties");
    	props = new Properties();
    	try {
			props.load(stream);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
    	try {
			Class.forName(props.getProperty("driverClassName"));
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
    public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
    	/*
    	 * 1、加载配置文件
    	 * 2、加载驱动类
    	 * 3、获取数据库连接
    	 */
    	return DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"),props.getProperty("password"));
    }
}
